package com.diao.flink.windowAndTimer;

import org.apache.flink.api.common.eventtime.*;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

/**
 * @author: chenzhidiao
 * @date: 2021/2/4 11:38
 * @description: 引入WaterMark来解决数据的乱序问题，只需要在生成watermark的时候延时5秒即可
 * @version: 1.0
 *
 * watermark只能用于window中
 * 且要使用EventTime
 *
 * TODO Watermark作为一种数据在流中流动，比如我们配置流watermark的延时为5秒，那么对于10秒时刻的watermark，它在15秒的时候才会到达
 *  并触发窗口的计算，而计算的边界就是10秒
 *
 * 结果：
 * (flink,2)
 * (flink,3)
 * (flink,1)
 */
public class WindowWordCountWithWatermark {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.setParallelism(1);

        //模仿延迟数据，在13秒时发送一条，16秒时一条，19秒时一条
        DataStreamSource<String> streamSource = env.addSource(new TestSourceDelayedWithTimestamp());

        streamSource.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {

            @Override
            public void flatMap(String s, Collector<Tuple2<String, Long>> collector) throws Exception {
                //将每条数据按逗号分割，第一个字段时单词，第二个字段时时间戳
                String[] fields = s.split(",");
                collector.collect(Tuple2.of(fields[0],Long.valueOf(fields[1])));
            }
        }).assignTimestampsAndWatermarks( WatermarkStrategy //定义水位策略
                .forGenerator((ctx) -> new PeriodicWatermarkGenerator())
                .withTimestampAssigner((ctx) -> new TimeStampExtractor()))
                .keyBy(tuple -> tuple.f0)
                .window(SlidingEventTimeWindows.of(Time.seconds(10), Time.seconds(5)))
                .process(new SumProcessWindowFunction1())
                .print().setParallelism(1);

        env.execute("wordCountWithWindow");

    }


    /**
     * watermark的生成方式，延迟5秒
     */
    private static class PeriodicWatermarkGenerator implements WatermarkGenerator<Tuple2<String, Long>> {


        //每来一条事件数据调用一次，可以检查或者记录事件的时间戳，或者也可以基于事件数据本身去生成 watermark
        @Override
        public void onEvent(Tuple2<String, Long> event, long eventTimestamp, WatermarkOutput watermarkOutput) {
            //System.out.println(eventTimestamp);
        }

        //周期性地效用，也许会生成新的 watermark，也许不会
        //调用此方法生成 watermark 的时间间隔由 ExecutionFonfig.getAutoWatermarkInterval()决定
        @Override
        public void onPeriodicEmit(WatermarkOutput watermarkOutput) {
            watermarkOutput.emitWatermark(new Watermark(System.currentTimeMillis()-5000));

        }
    }

    /**
     * 指定EventTime字段
     */
    private static class TimeStampExtractor implements TimestampAssigner<Tuple2<String,Long>> {

        @Override
        public long extractTimestamp(Tuple2<String, Long> element, long recordTimestamp) {
            return element.f1;
        }
    }
}
